
package br.com.merks.controller;

import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.view.Results;
import br.com.merks.Constants;
import br.com.merks.controller.component.SecurityModule;
import br.com.merks.model.User;
import br.com.merks.service.MarketService;
import br.com.merks.service.UserService;
import br.com.yx.vraptor.security.Protect;

@Resource
public class IndexController {
	private UserService dao = null;
	private Result result = null;
	private MarketService questionService = null;
	public IndexController(UserService dao, MarketService questionService, Result result, SecurityModule loginHandler) {
			this.result = result;
			 this.dao = dao;
			 this.questionService = questionService;
	}
	@Path("/")
	@Protect(annonymous=true)
	public void index() {
		result.include("users", dao.list());
		result.include("lastMarkets", questionService.listLastMarkets(10));
		result.include("topMarkets", questionService.listTopMarkets(10));
	}
	@Protect(roles="ADMIN")
	@Path(value = "deleteAll")
	public void delete() {
		dao.deleteAll();
		questionService.deleteAll();
		this.result.use(Results.logic()).forwardTo(IndexController.class).index();
	}
	@Protect(roles="ADMIN")
	@Path(value = "marketUser")
	public void createMarketUser() {
		User u = new User();
		u.setId(Constants.MARKET_USER);
		u.setName("Jarbas Malabi");
		u.setEmail("malabi@merkato.com.br");
		u.setAgeDay(1);
		u.setAgeMonth(1);
		u.setAgeYear(1991);
		u.setBlog("noblog");
		u.setFacebook("nofacebook");
		u.setGender(1);
		u.setOrkut("noorkut");
		u.setTwitter("notwitter");
		u.setCountry("nocountry");
		u.setCity("nocity");
		u.setEmailValid(1);
		u.setEmailValidToken("valid");
		u.setState("nostate");
		u.setLogin("malabi");
		u.setPassword("malabi");
		this.dao.registerUser(u);
		this.result.use(Results.logic()).forwardTo(IndexController.class).index();
	}

	@Path("/error401")
	public void error401() {
		
	}
	public void logout() {
	
		this.result.use(Results.logic()).forwardTo(IndexController.class).index();
	}
}